package Chapter05.Automobilemanagement;

import Chapter05.Cars.Bus;
import Chapter05.Cars.Car;
import Chapter05.Cars.MotoVehicle;

import java.util.Scanner;

public class CarBusiness {
    private String name = "";
    private String phone = "999999999";
    private int pos = 0;
    Scanner input = new Scanner(System.in);
    MotoVehicle mo[] = new MotoVehicle[100];
    RentalHistory ren[] = new RentalHistory[1000];

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }

    public CarBusiness() {
        mo[0] = new Car("宝马1", "11111", "轿车", 800, 5,true);
        mo[1] = new Car("宝马2", "22222", "轿车", 600, 15,true);
        mo[2] = new Car("别克3", "33333", "轿车", 300, 25,true);
        mo[3] = new Car("别克4", "44444", "轿车", 600, 10,true);
        mo[4] = new Bus("金杯1", "55555", "客车", 800, 15,true);
        mo[5] = new Bus("金龙1", "66666", "客车", 800, 30,true);
        mo[6] = new Bus("金龙2", "77777", "客车", 100, 35,true);
        mo[7] = new Bus("金龙2", "88888", "客车", 300, 15,true);
        mo[8] = new Bus("金龙4", "99999", "客车", 900, 17,true);
        mo[9] = new Bus("宝马3", "10101", "客车", 800, 7,true);
        pos = 10;
    }

    //购买新车
    public void addCar(String brand, String plate, String type, int seatnum, int dayrent, boolean state) {
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                if (type.equals("轿车")) {
                    mo[i] = new Car(brand, plate, type, seatnum, dayrent, state);
                } else {
                    mo[i] = new Bus(brand, plate, type, seatnum, dayrent, state);
                }
                System.out.println("成功购买！新车信息如下:");
                {

                    System.out.println(mo[i].toString());
                    return;
                }
            }
            System.out.println("购买失败，车库已满!");
        }
    }

    //显示信息
    public void displayinformation() {
        for (int i = 0; i < pos; i++) {
            System.out.println(mo[i].toString());
           // System.out.println(mo[i].getType() + "\t" + mo[i].getBrand() + "\t" + "车牌:" + mo[i].getPlate() + "\t" + mo[i].getSeatnum() + "座" + "\t" + ",日租金:" + mo[i].getDayrent() + "元" + "\t" + mo[i].isState());
        }
        //if(mo[0]==null){
        //  System.out.println("当前无记录!");
        // }
        //  for(int i = 0; i < mo.length; i++) {
        //     if (mo[i] == null) {
        //         break;
        //     } else {
        //          System.out.println(mo[i].getType() + "\t" + mo[i].getBrand() + "\t" + "车牌:" + mo[i].getPlate() + "\t" + mo[i].getSeatnum() + "座" + "\t" + ",日租金:" + mo[i].getDayrent() + "元" + "\t" + mo[i].getState());
        //      }
        //
        //   }
    }

//    //显示信息
//    public void showRentCarList() {
//        for (int i = 0; i < mo.length; i++) {
//            if (mo[i] == null) {
//                break;
//            } else {
//                System.out.println(mo[i].toString());
//                //System.out.println(mo[i].getType() + "\t" + mo[i].getBrand() + "\t" + "车牌:" + mo[i].getPlate() + "\t" + mo[i].getSeatnum() + "座" + "\t" + ",日租金:" + mo[i].getDayrent() + "元" + "\t" + mo[i].isState());
//            }
//        }
//    }

    public void showCar(String type) {
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                break;
            } else {
                if (mo[i].getType().equals(type)) {
                    System.out.println(mo[i].toString());
                    //System.out.println(mo[i].getType() + " " + mo[i].getBrand() + ",车牌:" + mo[i].getPlate() + " " + mo[i].getSeatnum() + "座," + ",日租金:" + mo[i].getDayrent() + "元," + mo[i].isState());
                }
            }
        }
    }

    public boolean rentCar1(String plate) {
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                break;
            } else {
                if (mo[i].getPlate().equals(plate) && mo[i].isState()) {
                    System.out.println("您要租的车信息如下:");
                    System.out.println(mo[i].toString());
                    //System.out.println(mo[i].getType() + " " + mo[i].getBrand() + ",车牌:" + mo[i].getPlate() + ",日租金:" + mo[i].getDayrent() + "元" + mo[i].isState());
                    return true;
                } else if (mo[i].getPlate().equals(plate) && mo[i].isState()) {
                    System.out.println("你选择的这个车已经被租出去了");
                    return false;
                }
            }
        }
        System.out.println("您选择的这辆车不存在");
        return false;
    }

    public void rentCar2(String plate, int day) {
        for (int i = 0; i < mo.length; i++) {
            if (mo[i].getPlate().equals(plate) && mo[i].isState()) {
                mo[i].money(day);
                for (int j = 0; j < ren.length; j++) {
                    if (ren[j] == null) {
                        ren[j] = new RentalHistory(plate, "出租");
                        ren[j].setDay(day);
                        break;
                    }
                }
                return;
            }
        }
    }

    //查看车辆可租
    public int showCarcount(String type) {
        int counts = 0;
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                break;
            } else {
                if (mo[i].getType().equals(type)) ;
                counts++;
            }
        }
        return counts;
    }

    public int getCarnum() {
        int num = 0;
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                return num;
            }
            if (mo[i] != null && mo[i].isState()) ;
            num++;
        }
        return num;
    }

    //还车
    public boolean Returnthecar(String plate) {
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                break;
            } else if (mo[i].getPlate().equals(plate) && mo[i].isState()) {
                System.out.println("这辆车当前没有出租出去");
                return false;
            } else {
                if (mo[i].isState()) {
                    mo[i].isState();
                    for (int j = 0; j < ren.length; j++) {
                        if (ren[j] == null) {
                            ren[j] = new RentalHistory(plate, "收回");
                            break;
                        }
                    }
                    return true;
                }
            }
        }
        System.out.println("归还失败，请核对信息!");
        return false;
    }

    //报废
    public boolean scrap(String plate) {
        for (int i = 0; i < mo.length; i++) {
            if (mo[i] == null) {
                break;
            } else if (mo[i].getPlate().equals(plate)) {
                int j = i;
                for (j = i; j < mo.length - 1; j++) {
                    mo[j] = mo[j + 1];
                }
                mo[mo.length - 1] = null;
                System.out.println("车辆报废成功");
                return true;
            }
        }
        System.out.println("归还失败，请核对信息!");
        return false;
    }
}
